package ece.pdx.finalproject.roleplay;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;



public class DataBaseHandler
{
	static private SQLiteDatabase db;
	private DBHelper dbHelper;
	
	public DataBaseHandler(Context context) 
	{
		dbHelper = new DBHelper(context);
		db = dbHelper.getWritableDatabase();
	}
	
	// Close the db
	public void close() 
	{
		db.close();
	}
	
	/**
	 * Create new TODO object
	 * @param todoText
	 */
	public void createTodo(ToDoList list) 
	{
		db = dbHelper.getWritableDatabase();
     
		ContentValues contentValues = new ContentValues();
		contentValues.put("todo", list.getText());
		contentValues.put("role", list.getRole());
		contentValues.put("date", list.getDate());
	    // Insert into DB
		db.insert("todos", null, contentValues);
	}
	
	/**
	 * Delete TODO object
	 * @param todoId
	 */
	public void deleteTodo(int todoId) 
	{
		// Delete from DB where id match
		db.delete("todos", "_id = " + todoId, null);
	}
	
	/**
	 * Get all TODOs.
	 * @return
	 */
	public List<ToDoList> getTodos() 
	{
		List<ToDoList> todoList = new ArrayList<ToDoList>();
		
		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id","todo","role","date"};
		
		// Query the database
		Cursor cursor = db.query("todos", tableColumns, null, null, null, null, null);
		cursor.moveToFirst();
		
		// Iterate the results
	    while (!cursor.isAfterLast()) 
	    {
	    	ToDoList todo = new ToDoList();
	    	// Take values from the DB
	    	todo.setId(cursor.getInt(0));
	    	todo.setText(cursor.getString(1));
	        todo.setRole(cursor.getString(2));
	        todo.setDate(cursor.getString(3));
	    	// Add to the DB
	    	todoList.add(todo);
	    	
	    	// Move to the next result
	    	cursor.moveToNext();
	    }
		
		return todoList;
	}
	/**
	 * Get Work TODOs.
	 * @return
	 */
	public List<ToDoList> getWorkTodos() 
	{
		List<ToDoList> todoList = new ArrayList<ToDoList>();
		
		
		// Query the database
		Cursor cursor = db.rawQuery("SELECT * FROM todos WHERE role= ?",new String[]{"Work"});
		cursor.moveToFirst();
		
		// Iterate the results
	    while (!cursor.isAfterLast()) 
	    {
	    	ToDoList todo = new ToDoList();
	    	// Take values from the DB
	    	todo.setId(cursor.getInt(0));
	    	todo.setText(cursor.getString(1));
	        todo.setRole(cursor.getString(2));
	        todo.setDate(cursor.getString(3));
	    	// Add to the DB
	    	todoList.add(todo);
	    	
	    	// Move to the next result
	    	cursor.moveToNext();
	    }
		
		return todoList;
	}
	/**
	 * Get Family TODOs.
	 * @return
	 */
	public List<ToDoList> getFamilyTodos() 
	{
		List<ToDoList> todoList = new ArrayList<ToDoList>();
		
		
		// Query the database
		Cursor cursor = db.rawQuery("SELECT * FROM todos WHERE role= ?",new String[]{"Family"});
		cursor.moveToFirst();
		
		// Iterate the results
	    while (!cursor.isAfterLast()) 
	    {
	    	ToDoList todo = new ToDoList();
	    	// Take values from the DB
	    	todo.setId(cursor.getInt(0));
	    	todo.setText(cursor.getString(1));
	        todo.setRole(cursor.getString(2));
	        todo.setDate(cursor.getString(3));
	    	// Add to the DB
	    	todoList.add(todo);
	    	
	    	// Move to the next result
	    	cursor.moveToNext();
	    }
		
		return todoList;
	}	
	/**
	 * Get I TODOs.
	 * @returnmportant
	 */
	public List<ToDoList> getImportantTodos() 
	{
		List<ToDoList> todoListOne = new ArrayList<ToDoList>();
		List<ToDoList> todoListTwo = new ArrayList<ToDoList>();
		List<ToDoList> todoListThree = new ArrayList<ToDoList>();
		List<ToDoList> todoList = new ArrayList<ToDoList>();
        Calendar calToday = new GregorianCalendar();
        if(calToday.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||calToday.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)
        {
        	todoListOne=getFamilyTodos();
        	todoListTwo=getFriendTodos();
        	todoListThree=getWorkTodos();
        }
        else
        {
        	todoListOne=getWorkTodos();
        	todoListTwo=getFamilyTodos();
        	todoListThree=getFriendTodos();        	
        }
        todoList.addAll(todoListOne);
        todoList.addAll(todoListTwo);
        todoList.addAll(todoListThree);
		return todoList;
	}
	/**
	 * Get Urgent  TODOs.
	 * @return
	 */
	public List<ToDoList> getUrgentTodos() 
	{
		List<ToDoList> todoList=new ArrayList<ToDoList>();
		List<ToDoList> todoListUrgent = new ArrayList<ToDoList>();
		List<ToDoList> todoListNotUrgent=new ArrayList<ToDoList>();
		
		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id","todo","role","date"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
      
        
        Calendar calToday = new GregorianCalendar();
        int wk_yr_today = calToday.get(Calendar.WEEK_OF_YEAR);
        Date dueDate = new Date();
        Calendar calDueDate = new GregorianCalendar(); 

        
        // Query the database
		Cursor cursor = db.query("todos", tableColumns, null, null, null, null, null);
		cursor.moveToFirst();
		
		// Iterate the results
	    while (!cursor.isAfterLast()) 
	    {
	    	ToDoList todo = new ToDoList();
	    	// Take values from the DB
	    	todo.setId(cursor.getInt(0));
	    	todo.setText(cursor.getString(1));
	        todo.setRole(cursor.getString(2));
	        todo.setDate(cursor.getString(3));
	    	// Add to the DB
	        try 
	        {  
	        	dueDate = dateFormat.parse(todo.getDate());
	        } 
	        catch (ParseException e) 
	        {  
	            e.printStackTrace();  
	        }
	        
	        calDueDate.setTime(dueDate);
	        int wk_yr_duedate = calDueDate.get(Calendar.WEEK_OF_YEAR);
	        if(wk_yr_today == wk_yr_duedate)
	        {
	        	todoListUrgent.add(todo);
	        }
	        else
	        
	        {
	        	todoListNotUrgent.add(todo);
	        }
	    	
	    	// Move to the next result
	    	cursor.moveToNext();
	    }
		todoList.addAll(todoListUrgent);
		todoList.addAll(todoListNotUrgent);
		return todoList;
	}
	/**
	 * Get Friend TODOs.
	 * @return
	 */
	public List<ToDoList> getFriendTodos() 
	{
		List<ToDoList> todoList = new ArrayList<ToDoList>();
		
		
		// Query the database
		Cursor cursor = db.rawQuery("SELECT * FROM todos WHERE role= ?",new String[]{"Friend"});
		cursor.moveToFirst();
		
		// Iterate the results
	    while (!cursor.isAfterLast()) 
	    {
	    	ToDoList todo = new ToDoList();
	    	// Take values from the DB
	    	todo.setId(cursor.getInt(0));
	    	todo.setText(cursor.getString(1));
	        todo.setRole(cursor.getString(2));
	        todo.setDate(cursor.getString(3));
	    	// Add to the DB
	    	todoList.add(todo);
	    	
	    	// Move to the next result
	    	cursor.moveToNext();
	    }
		
		return todoList;
	}
	public int updateToDo(ToDoList todo,int id)
	{
		db = dbHelper.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("todo", todo.getText());
		contentValues.put("role", todo.getRole());
		contentValues.put("date", todo.getDate());
		// updating row
	    return db.update("todos", contentValues, "_id" + " = ?",
	            new String[] { String.valueOf(id) });
	}
	
}
